<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

class BaiduFanyi{
    public function fanyi($res,$from="auto",$to="auto")
    {
     $url="http://api.fanyi.baidu.com/api/trans/vip/translate";
     $q=urlencode($res);
     $appid="20190628000311599";
     $salt= rand(10000,99999);
     $mishi="Yze72gukj0hKVmaLfmbL";
     $sign= md5($appid.$res.$salt.$mishi);
     
     $url="http://api.fanyi.baidu.com/api/trans/vip/translate?q=".$q."&from=".$from."&to=".$to."&appid=".$appid."&salt=".$salt."&sign=".$sign;
	 $obj = $this->crul($url);
     $r= json_decode($obj,true);
     $result = $r['trans_result']; 
	 foreach($result as $key=>$val)
	 {
		$data[$key]['ytext'] = $val['src'];		
		$data[$key]['ftext'] = $val['dst'];		 
	 }
	 
	 return $data;
    }
    
    public function crul($url){
        $ch= curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
        $file_contect=curl_exec($ch);
        curl_close($ch);
        file_put_contents("ccc", $file_contect);
        return $file_contect;
        
    }
}
?>